﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net.Mail;

namespace ThoiTrangT2B
{
    public partial class ThanhToan : System.Web.UI.Page
    {
        private static Dictionary<Int32, Int32> lstGioHang;
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnDone_Click(object sender, EventArgs e)
        {
            SqlTransaction Mytran = SqlHelper.BeginTransaction();
            try
            {
                lstGioHang = (Dictionary<Int32, Int32>)Session["GioHang"];
                string sql = "INSERT INTO dbo.tblT2BHoaDon(Hoten,Diachi,SDT,Email,Ghichu,TrangThai,CreateDate) " +
                             "VALUES(@Hoten,@Diachi,@SDT,@Email,@Ghichu,0,GETDATE()) SELECT IDENT_CURRENT('tblT2BHoaDon')";
                List<SqlParameter> arrParams = new List<SqlParameter>();
                arrParams.Add(SqlHelper.CreateParameterObject("@HoTen", SqlDbType.NVarChar, ParameterDirection.Input, txtHoTen.Text));
                arrParams.Add(SqlHelper.CreateParameterObject("@DiaChi", SqlDbType.NVarChar, ParameterDirection.Input, txtDiaChi.Text));
                arrParams.Add(SqlHelper.CreateParameterObject("@SDT", SqlDbType.NVarChar, ParameterDirection.Input, txtSDT.Text));
                arrParams.Add(SqlHelper.CreateParameterObject("@Email", SqlDbType.NVarChar, ParameterDirection.Input, txtEmail.Text));
                arrParams.Add(SqlHelper.CreateParameterObject("@GhiChu", SqlDbType.NVarChar, ParameterDirection.Input, txtGhiChu.Text));
                int HoadonID = -1;
                HoadonID = Convert.ToInt32(SqlHelper.ExecuteScalar(CommandType.Text, sql, arrParams));
                arrParams.Clear();
                sql = "INSERT INTO dbo.tblT2BCTHD( HDID, SPID, SL )VALUES  ( @HDID, @SPID, @SL)";
                foreach (var item in lstGioHang)
                {
                    arrParams.Add(SqlHelper.CreateParameterObject("@HDID", SqlDbType.Int, ParameterDirection.Input, HoadonID));
                    arrParams.Add(SqlHelper.CreateParameterObject("@SPID", SqlDbType.Int, ParameterDirection.Input, item.Key));
                    arrParams.Add(SqlHelper.CreateParameterObject("@SL", SqlDbType.Int, ParameterDirection.Input, item.Value));
                    SqlHelper.ExecuteNonQuery(CommandType.Text, sql, arrParams);
                    arrParams.Clear();
                }
                Mytran.Commit();
                SentMail(txtEmail.Text);
            }
            catch (Exception ex)
            {
                Mytran.Rollback();
            }
        }

        private void SentMail(string mail)
        {
            MailMessage message = new MailMessage();
            SmtpClient smtpClient = new SmtpClient();
            string msg = string.Empty;
            string from = "thoitrangt2b@gmail.com";
            string body = SqlHelper.ExecuteScalar(CommandType.Text, "select GhiChu from tblT2BDanhMuc where MaDM = 'email'").ToString();
            try
            {
                MailAddress fromAddress = new MailAddress(from);
                message.From = fromAddress;
                message.To.Add(mail);

                message.Subject = "Thông báo đặt hàng thành công trên ThoiTrangT2B.com";
                message.IsBodyHtml = false;
                message.Body = body;
                smtpClient.Host = "smtp.gmail.com";   // We use gmail as our smtp client
                smtpClient.Port = 587;
                smtpClient.EnableSsl = true;
                smtpClient.UseDefaultCredentials = true;
                smtpClient.Credentials = new System.Net.NetworkCredential("thoitrangt2b", "123456789a@");

                smtpClient.Send(message);
                msg = "Successful<BR>";
            }
            catch (Exception ex)
            {
                msg = ex.Message;
            }
            Session.Remove("GioHang");
            Response.Redirect("Success.aspx");
        }
    }
}
